/******************************************************************************
 * Filename : uc_mem.h
 * Created  : 2010-11-06 by franco.yin
 * Description -
 * 
 ******************************************************************************/

#ifndef	__UC_MEM_H__
#define	__UC_MEM_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include <stdlib.h>

#include "uc.h"

//#define UC_MEM_DEBUG 1

#define uc_malloc(size) _uc_malloc(size, __FILE__, __func__, __LINE__)
#define uc_malloc_memset(size) _uc_malloc_memset(size, __FILE__, __func__, __LINE__)
#define uc_free(p) _uc_free(p, __FILE__, __func__, __LINE__)

extern int uc_mem_dump_add(void *p, int size, char *file, const char * func, int line);
extern int uc_mem_dump_del(void *p, char *file, const char * func, int line);
extern void uc_mem_dump_show();

static inline void *
_uc_malloc(int size, char *file, const char *func, int line) 
{
	void *p = NULL;

	p = malloc(size);
#if UC_MEM_DEBUG
	if(p != NULL) {
		uc_mem_dump_add(p, size, file, func, line);
	}
#endif
	return p;
}

static inline void *
_uc_malloc_memset(int size, char *file, const char *func, int line) 
{
	void *p = NULL;

	p = malloc(size);
	if(p != NULL) {
		memset(p, 0, size);
#if UC_MEM_DEBUG
		uc_mem_dump_add(p, size, file, func, line);
#endif
	}
	return p;
}

static inline void
_uc_free(void *p, const char *file, const char *func, int line) 
{
#if UC_MEM_DEBUG
	uc_mem_dump_del(p, file, func, line);
#endif
	free(p);
}

#ifdef __cplusplus
}
#endif

#endif

